package com.fancy.social.service.impl;

import com.fancy.social.dto.DynamicCreateDTO;
import com.fancy.social.entity.UserDynamic;
import com.fancy.social.mapper.UserDynamicMapper;
import com.fancy.social.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class DynamicServiceImpl implements DynamicService {

    @Autowired
    private UserDynamicMapper userDynamicMapper;

    @Override
    public void createDynamic(Long userId, DynamicCreateDTO dynamicCreateDTO) {
        UserDynamic userDynamic = new UserDynamic();
        userDynamic.setUserId(userId);
        userDynamic.setContent(dynamicCreateDTO.getContent());
        userDynamic.setDynamicType(dynamicCreateDTO.getDynamicType());
        userDynamic.setTargetId(dynamicCreateDTO.getTargetId());
        userDynamic.setTargetType(dynamicCreateDTO.getTargetType());
        userDynamic.setImages(dynamicCreateDTO.getImages());
        userDynamic.setIsPublic(dynamicCreateDTO.getIsPublic());
        userDynamic.setCreateTime(LocalDateTime.now());
        userDynamic.setLikeCount(0);
        userDynamic.setCommentCount(0);
        userDynamic.setShareCount(0);
        userDynamicMapper.insert(userDynamic);
    }

    @Override
    public void deleteDynamic(Long userId, Long dynamicId) {
        UserDynamic userDynamic = userDynamicMapper.selectById(dynamicId);
        if (userDynamic != null && userDynamic.getUserId().equals(userId)) {
            userDynamicMapper.deleteById(dynamicId);
        }
    }
}

